Import yimWork

' This is my juicy sprite system!
' It is inspired by the awesome talk given by Martin Jonasson & Petri Purho
' Seen here: http://www.youtube.com/watch?feature=player_embedded&v=Fy0aCDmgnxg

Class yimJuicyBase
	Field spr:yimSprite
	
	Method Update:Bool()
		' This shizzle gets overridden
	End
End

Class yimJuicyTranslate Extends yimJuicyBase
	Field speed:yimVec2D
	Field accel:yimVec2D
	Field initAccel:yimVec2D
	Field target:yimVec2D
	Field direction:yimVec2D
	Field endTime:Float
	
End

Class yimJuicyTranslateSpring Extends yimJuicyTranslate
	Field passed:Bool = False
	
	Method New(startX:Float, startY:Float, endX:Float, endY:Float, velocity:Float, acceleration:Float)
		target = New yimVec2D(endX, endY)
		Local ang:Float = ATan2(endX-startX, endY-startY)
		initAccel = New yimVec2D(acceleration*Sin(ang), acceleration*Cos(ang))
		accel = New yimVec2D(0, 0)
		speed = New yimVec2D(velocity*Sin(ang), velocity*Cos(ang))
		direction = New yimVec2D(ValDir(startX, endX), ValDir(startY, endY))
	End
	
	Method Update:Bool()
		speed.x += accel.x
		speed.y += accel.y

		spr.MoveXY(speed.x, speed.y)
		
		Local dirx:Int = ValDir(spr.pos.x, target.x)
		Local diry:Int = ValDir(spr.pos.y, target.y)
		
		If dirx <> Int(direction.x) Or diry <> Int(direction.y)
			If passed
				spr.SetPosition(target.x, target.y)
				Return False
			EndIf
			passed = True
			accel.x = initAccel.x
			accel.y = initAccel.y
		EndIf
		
		
		
		direction.Set(dirx, diry)
		
		Return True
	End
End

Class yimJuicyTranslateBounce Extends yimJuicyTranslate
	Field passed:Bool = False
	
	Method New(startX:Float, startY:Float, endX:Float, endY:Float, velocity:Float, acceleration:Float)
		target = New yimVec2D(endX, endY)
		Local ang:Float = ATan2(endX-startX, endY-startY)
		initAccel = New yimVec2D(acceleration*Sin(ang), acceleration*Cos(ang))
		accel = New yimVec2D(0, 0)
		speed = New yimVec2D(velocity*Sin(ang), velocity*Cos(ang))
		direction = New yimVec2D(ValDir(startX, endX), ValDir(startY, endY))
	End
	
	Method Update:Bool()
		speed.x += accel.x
		speed.y += accel.y

		spr.MoveXY(speed.x, speed.y)
		
		Local dirx:Int = ValDir(spr.pos.x, target.x)
		Local diry:Int = ValDir(spr.pos.y, target.y)
		
		If dirx <> Int(direction.x)
			speed.x *= -0.6
		EndIf
		
		If diry <> Int(direction.y)
			speed.y *= -0.6
		EndIf
		
		direction.Set(dirx, diry)
		
		Return True
	End
End